/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.util;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * @author ETCORE GroupTalk Developers
 * 
 */
public class ImageUtil {
	public static void ZoomImage(String sourceFile, String targetFile,
			int width, int height) {
		java.io.File file = new java.io.File(sourceFile);
		System.out.println("zoomWidth:" + width + ",zoomHeight:" + height);
		Image src = null;
		try {
			src = javax.imageio.ImageIO.read(file);

			BufferedImage bufferedImage = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);

			bufferedImage.getGraphics().drawImage(src, 0, 0, width, height,
					null);
			FileOutputStream newimage = new FileOutputStream(targetFile);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
			encoder.encode(bufferedImage);
			newimage.close();
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	public static void cut(String srcPath, String targetPath, int x, int y,
			int width, int height) throws IOException {
		/*System.out.println("cutWidth:" + width + ",cutHeight:" + height
				+ ",cutX:" + x + ",cutY:" + y);*/
		FileInputStream is = null;
		ImageInputStream iis = null;
		BufferedImage image = null;

		java.io.File file = new java.io.File(srcPath);

		try {

			image = ImageIO.read(file);
			if (image != null) {
				int realX=x < 0 ? 0 : x,realY=y < 0 ? 0 : y,realWidth=image.getWidth(),realHeight=image.getHeight(); 
			if(width>realWidth-x)	{
				realX=(realWidth-x)*x/width;
				realWidth=realWidth-x;
			}
			else{
				realX= (width-realWidth)*x/width;
				realX=realX<0?0:realX;
				realWidth=width;
			}
			if(height>realHeight-y)	{
				realY=(realHeight-y)*y/height;
				realHeight=realHeight-y;
				
			}else {
				
				realY= (height-realHeight)*y/height;
				realHeight=height;
				realY=realY<0?0:realY;
			}
			/*System.out.println("realWidth:" + realWidth + ",realHeight:" + realHeight
					+ ",realX:" + realX + ",realY:" + realY);*/
				is = new FileInputStream(srcPath);

				/**//*
					 * 返回包含所有当前已注册 ImageReader 的 Iterator，这些 ImageReader
					 * 声称能够解码指定格式。 参数：formatName - 包含非正式格式名称 .（例如 "jpeg" 或
					 * "tiff"）等 。
					 */
				Iterator<ImageReader> it = ImageIO
						.getImageReadersByFormatName("jpg");
				ImageReader reader = it.next();
				// 获取图片流
				iis = ImageIO.createImageInputStream(is);

				/**//*
					 * <p>iis:读取源.true:只向前搜索 </p>.将它标记为 ‘只向前搜索’。
					 * 此设置意味着包含在输入源中的图像将只按顺序读取，可能允许 reader
					 * 避免缓存包含与以前已经读取的图像关联的数据的那些输入部分。
					 */
				reader.setInput(iis, true);

				/**//*
					 * <p>描述如何对流进行解码的类<p>.用于指定如何在输入时从 Java Image I/O
					 * 框架的上下文中的流转换一幅图像或一组图像。用于特定图像格式的插件 将从其 ImageReader 实现的
					 * getDefaultReadParam 方法中返回 ImageReadParam 的实例。
					 */
				ImageReadParam param = reader.getDefaultReadParam();

				/**//*
					 * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域，通过 Rectangle 对象
					 * 的左上顶点的坐标（x，y）、宽度和高度可以定义这个区域。
					 */
				Rectangle rect = new Rectangle(realX, realY,
						realWidth, realHeight);

				// 提供一个 BufferedImage，将其用作解码像素数据的目标。
				param.setSourceRegion(rect);

				/**//*
					 * 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象，并将 它作为一个完整的
					 * BufferedImage 返回。
					 */
				BufferedImage bi = reader.read(0, param);

				// 保存新图片
				ImageIO.write(bi, "jpg", new File(targetPath));
			}
		}

		finally {
			if (is != null)
				is.close();
			if (iis != null)
				iis.close();
		}

	}
}
